﻿using System;
using System.Windows.Forms;
using RVEAd.Business;
using RVEAd.Business.Model;
using RVEAd.Helpers;
using RVEAd.Models;

namespace RVEAd.Forms
{
    public partial class FormPlanifica : Form
    {
        private RveBL _rveBl;

        public FormPlanifica()
        {
            InitializeComponent();
            Load += FormPlanifica_Load;
        }

        private void ClearInfoLabels()
        {
            lblClient.Text = "";
            lblSpot.Text = "";
            lblPlanDate.Text = "";
            lblAparitii.Text = "";
        }

        private void SetInfoLabels()
        {
            // info - selected client
            if (SelectedClient != null && !string.IsNullOrEmpty(SelectedClient.Nume))
            {
                lblClient.Text = string.Format("Clientul ales: {0}", SelectedClient.Nume);
            }

            // info - selected spot
            if (SelectedSpot != null && !string.IsNullOrEmpty(SelectedSpot.Description))
            {
                lblSpot.Text = string.Format("Spotul ales: {0}", SelectedSpot.Description);
            }
            
            // info - plan date
            lblPlanDate.Text = string.Format("Luna aleasa: {0} - {1}", datePickerDataPlan.Value.Month, datePickerDataPlan.Value.Year);

            // info - nr aparitii
            lblAparitii.Text = !string.IsNullOrEmpty(tbNumarAparitii.Text) ? string.Format("Numar de aparitii: {0}", tbNumarAparitii.Text) : "";
        }

        #region void btnAlegeClient_Click(object sender, EventArgs e)

        private void BtnAlegeClientClick(object sender, EventArgs e)
        {
            var alegeClientForm = new FormAlegeClient();
            
            alegeClientForm.FormClosed += (oo, ss) => 
            { 
                SelectedClient = alegeClientForm.SelectedClient;
                SetInfoLabels();
            };
            
            alegeClientForm.ShowDialog();
        }

        #endregion

        #region void btnAlegeSpot_Click(object sender, EventArgs e)

        private void btnAlegeSpot_Click(object sender, EventArgs e)
        {
            var alegeSpotForm = new FormAlegeSpot();
            alegeSpotForm.FormClosed += (oo, ss) => 
            { 
                SelectedSpot = alegeSpotForm.SelectedSpotFile;
                SetInfoLabels();
            };
            alegeSpotForm.ShowDialog();
        }

        #endregion

        public Client SelectedClient { get; set; }
        public SpotFile SelectedSpot { get; set; }


        private void datePickerDataPlan_ValueChanged(object sender, EventArgs e)
        {
            SetInfoLabels();
            SetAppObjectDate();
        }

        private void FormPlanifica_Load(object sender, EventArgs e)
        {
            ClearInfoLabels();

            _rveBl = new RveBL();

            SetAppObjectDate();
        }

        private void SetAppObjectDate()
        {
            // calculate days in month
            lblZileInLuna.Text = string.Format("{0}", Common.GetDaysNumberInMonth(datePickerDataPlan.Value));
        }

        private void tbNumarAparitii_TextChanged(object sender, EventArgs e)
        {
            int nrAparitii;
            int.TryParse(tbNumarAparitii.Text, out nrAparitii);

            int nrZileInLuna;
            int.TryParse(lblZileInLuna.Text, out nrZileInLuna);

            double nr = 0;
            if (nrAparitii > 0 && nrZileInLuna > 0)
            {
                nr = nrAparitii / nrZileInLuna;
            }

            lblNumarAparitii.Text = nr.ToString();
            SetInfoLabels();
        }

        private void btnSalveazaPlan_Click(object sender, EventArgs e)
        {
            if (CheckParameters())
            {
                SaveAdPlan(datePickerDataPlan.Value, SelectedClient, SelectedSpot);
            }
        }

        private bool CheckParameters()
        {
            if (datePickerDataPlan.Value == null)
            {
                MessageBox.Show("Va rog selectati o data !", "Eroare", MessageBoxButtons.OK);
                return false;
            }

            if (SelectedClient == null)
            {
                MessageBox.Show("Va rog selectati un client !", "Eroare", MessageBoxButtons.OK);
                return false;
            }

            if (SelectedSpot == null)
            {
                MessageBox.Show("Va rog selectati un spot !", "Eroare", MessageBoxButtons.OK);
                return false;
            }

            if (string.IsNullOrEmpty(tbNumarAparitii.Text))
            {
                MessageBox.Show("Va rog introduceti numarul de aparitii !", "Eroare", MessageBoxButtons.OK);
                return false;
            }

            return true;
        }

        private void SaveAdPlan(DateTime date, Client selectedClient, SpotFile spotFile)
        {
            int nrAparitii;
            int.TryParse(tbNumarAparitii.Text, out nrAparitii);

            if (nrAparitii > 0)
            {
                _rveBl.CreateCliendAd(selectedClient.ID, date, spotFile.Description, nrAparitii);
            }
            else
            {
                MessageBox.Show("Numarul de aparitii ");
            }

            Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}